package com.project.nhantvt.SessionBean;


import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.project.entity.Account;

/**
 * Session Bean implementation class UserBean
 */
@Stateless
@LocalBean
public class UserBean implements UserBeanRemote {

    /**
     * Default constructor. 
     */
	
	@PersistenceContext
	EntityManager em;
	 
    public UserBean() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public List<Account> GetAllAccount() {
		Query query = em.createQuery("From Account");
		@SuppressWarnings("unchecked")
		List <Account> lst = (List <Account>) query.getResultList();
		
		return lst;
	}

	@Override
	public boolean UpdateUser(Account tempAccount) {
		em.merge(tempAccount);		
		return true;
	}

	@Override
	public boolean UpdateTemp(int accID, boolean isVerified, boolean status) {
		Account acc = em.find(Account.class, accID);
		
		acc.setIsVerified(isVerified);
		acc.setStatus(status);
		
		em.merge(acc);
		return true;
	}

	@Override
	public String getRandomIMGPath(int accID) throws Exception {
		Query query = em.createNamedQuery("Account.findIDImage");
		query.setParameter("accID", accID);
		@SuppressWarnings("unchecked")
		List<String> result = query.getResultList();
		try {
			if (result.size() >= 1) {
				long i = Math.round(Math.random() * result.size() - 0.5);
				String s = result.get((int) i);
				return s;

			} else
				return "/uploaded/IDImages/notfound.jpg";
		} catch (Exception e) {
			e.printStackTrace();
			return "/uploaded/IDImages/notfound.jpg";
		}
	}
}
